package com.hmall.service;

import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.*;
import com.hmall.model.*;
import com.hdmall.business.common.util.MD5Util;
import com.hmall.vo.SysCustomerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.Date;

@Service
public class CustomerManage {
    private final
    SysCustomerMapper sysCustomerMapper;
    private final
    SysDeptMapper sysDeptMapper;
    private final
    MallDealerMapper mallDealerMapper;
    private final
    SysDeptCustomerMapper sysDeptCustomerMapper;
    private final
    SysUserMapper sysUserMapper;
    private final
    SysUserDeptMapper sysUserDeptMapper;
    private final
    SysUserRoleMapper sysUserRoleMapper;
    private final
    SysUserTempMapper sysUserTempMapper;

    @Autowired
    public CustomerManage(SysCustomerMapper sysCustomerMapper, SysDeptMapper sysDeptMapper, MallDealerMapper mallDealerMapper, SysDeptCustomerMapper sysDeptCustomerMapper, SysUserMapper sysUserMapper, SysUserDeptMapper sysUserDeptMapper, SysUserRoleMapper sysUserRoleMapper, SysUserTempMapper sysUserTempMapper) {
        this.sysCustomerMapper = sysCustomerMapper;
        this.sysDeptMapper = sysDeptMapper;
        this.mallDealerMapper = mallDealerMapper;
        this.sysDeptCustomerMapper = sysDeptCustomerMapper;
        this.sysUserMapper = sysUserMapper;
        this.sysUserDeptMapper = sysUserDeptMapper;
        this.sysUserRoleMapper = sysUserRoleMapper;
        this.sysUserTempMapper = sysUserTempMapper;
    }

    public void addUser(String username, String password,
                        Integer customer_id, String real_name, String auth_url, String license_url, String tax_cert_url, String id_face_url, String id_back_url){
        SysUser sysUser = new SysUser();
        SysUser checkUser = sysUserMapper.selectByUserName(username);

        if (checkUser != null) {
            ServerResponse.createByErrorMessage("用户名已存在");
            return;
        }

        Integer dept_id = sysDeptCustomerMapper.selectByCustomerId(customer_id);
        Integer dealer_id = mallDealerMapper.selectDealerId(dept_id);
        sysUser.setDealer_id(dealer_id);
        sysUser.setUsername(username);

        String md5Password = MD5Util.MD5EncodeUtf8(password);
        sysUser.setPassword(md5Password);
        sysUser.setPhone(username);
        sysUser.setQuestion(username);
        sysUser.setAvatar(
                "http://hd-yun.oss-cn-hangzhou.aliyuncs.com/imgupload/1542330401567.png?Expires=1857690395&OSSAccessKeyId=LTAI0AE4IXlOaSaX&Signature=9KAS%2FMn7D8buLbLkjdZKW9jX3Lc%3D");
        sysUser.setCreateTime(new Date());
        sysUser.setDelFlag("0");
        sysUser.setRealName(real_name);
        sysUserMapper.insert(sysUser);
        sysUser = sysUserMapper.selectByUserName(username);

        SysUserDept sysUserDept = new SysUserDept();
        sysUserDept.setDeptId(dept_id);
        sysUserDept.setUserId(sysUser.getUserId());
        sysUserDeptMapper.insert(sysUserDept);

        Integer user_id = sysUserMapper.selectIdByUsername(username);
        SysCustomer sysCustomer = sysCustomerMapper.selectByPrimaryKey(customer_id);
        SysUserRole sysUserRole = sysUserRoleMapper.selectRole(user_id);
        if (sysUserRole == null){
            SysUserRole sysUserRole1 = new SysUserRole();
            sysUserRole1.setRoleId(3);
            sysUserRole1.setUserId(user_id);
            sysUserRoleMapper.insert(sysUserRole1);
        }else {
            int status = 3;
            sysUserRoleMapper.updateStatus(user_id, status);
        }
        SysUserTemp sysUserTemp = new SysUserTemp();
        sysUserTemp.setUserId(user_id);
        sysUserTemp.setStatus(3);
        sysUserTemp.setLinkmanName(sysCustomer.getLinkmanName());
        sysUserTemp.setCreateTime(new Date());
        sysUserTemp.setOpinion("通过");
        sysUserTemp.setType(9);
        sysUserTemp.setAuthUrl(auth_url);
        sysUserTemp.setLicenseUrl(license_url);
        sysUserTemp.setTaxCertUrl(tax_cert_url);
        sysUserTemp.setIdFaceUrl(id_face_url);
        sysUserTemp.setIdBackUrl(id_back_url);
        sysUserTempMapper.insert(sysUserTemp);

        ServerResponse.createBySuccess("添加成功");
    }

    public String addCustomer(SysCustomerVo sysCustomerVo)throws Exception{
        try {
            Class stuCla = sysCustomerVo.getClass();// 得到类对象
            Field[] fs = stuCla.getDeclaredFields();//得到属性集合
            for (Field f : fs) {//遍历属性
                f.setAccessible(true); // 设置属性是可以访问的(私有的也可以)
                Object val = f.get(sysCustomerVo);// 得到此属性的值
                if(val.toString().isEmpty()) {
                    return "表格有字段为空";
                }
            }
            Integer count = sysCustomerMapper.checkBusName(sysCustomerVo.getBus_name());
            if (count > 0) {
                return "该客户已存在";
            }
            Integer count2 = sysDeptMapper.selectIdByName2(sysCustomerVo.getBus_name());
            if (count2!=null){
                return "部门已存在";
            }
            Integer dealer_id = mallDealerMapper.selectDealerId(sysCustomerVo.getDept_id());
            SysCustomer sysCustomer = new SysCustomer();
            sysCustomer.setBusName(sysCustomerVo.getBus_name());
            sysCustomer.setLinkmanName(sysCustomerVo.getLinkman_name());
            sysCustomer.setLinkmanTel(sysCustomerVo.getLinkman_tel());
            sysCustomer.setBusPersonName(sysCustomerVo.getBus_person_name());
            sysCustomer.setBusPersonId(sysCustomerVo.getBus_person_id());
            sysCustomer.setBusAddress(sysCustomerVo.getBus_address());
            sysCustomer.setCaptial(sysCustomerVo.getCaptial());
            sysCustomer.setValidPeriod(sysCustomerVo.getValid_period());
            sysCustomer.setBusiness(sysCustomerVo.getBusiness());
            sysCustomer.setRegNum(sysCustomerVo.getReg_num());
            sysCustomer.setInvoiceTitle(sysCustomerVo.getInvoice_title());
            sysCustomer.setInvoiceTitleAddr(sysCustomerVo.getInvoice_title_addr());
            sysCustomer.setTaxNum(sysCustomerVo.getTax_num());
            sysCustomer.setBankName(sysCustomerVo.getBank_name());
            sysCustomer.setBankNumber(sysCustomerVo.getBank_number());
            sysCustomer.setDealerId(dealer_id);
            sysCustomer.setCreateTime(new Date());
            sysCustomer.setAuthUrl(sysCustomerVo.getAuth_url());
            sysCustomer.setLicenseUrl(sysCustomerVo.getLicense_url());
            sysCustomer.setIdBackUrl(sysCustomerVo.getId_back_url());
            sysCustomer.setIdFaceUrl(sysCustomerVo.getId_face_url());
            sysCustomer.setTaxCertUrl(sysCustomerVo.getTax_cert_url());
            //添加客户
            sysCustomerMapper.insert(sysCustomer);
            //根据名称找到客户id
            Integer customer_id = sysCustomerMapper.selectCustomerByName(sysCustomerVo.getBus_name());
            //添加部门
            SysDept sysDept = new SysDept();
            sysDept.setName(sysCustomerVo.getBus_name());
            sysDept.setCreateTime(new Date());
            sysDept.setDelFlag("0");
            sysDept.setParentId(17);
            sysDeptMapper.insert(sysDept);
            //获取部门id
            Integer dept_id = sysDeptMapper.selectIdByName2(sysCustomerVo.getBus_name());
            //客户绑定部门
            SysDeptCustomer sysDeptCustomer = new SysDeptCustomer();
            sysDeptCustomer.setCustomerId(customer_id);
            sysDeptCustomer.setDeptId(dept_id);
            sysDeptCustomerMapper.insert(sysDeptCustomer);
            return "添加成功";
        }catch (NullPointerException e){
            return "空指针错误";
        }
    }
}
